﻿using System.Collections.Generic;

namespace etdc
{
    internal class KeyGenerator
    {
        public static IEnumerable<byte> Generate(IList<byte> baseKey)
        {
            var length = baseKey.Count;

            var index = 0;
            while (index < int.MaxValue)
            {
                var baseKeyByte = baseKey[index%length];
                var keyByte = baseKeyByte;

                // dont know how it works, lol
                const int magic1 = 1024;
                const int magic2 = 0x04;
                var magic3 = length;
                var k1 = (index/magic1);
                var k2 = (index%magic1)%(magic2*magic3);

                keyByte += (byte) (((k2 + (k1*magic2))/length)*magic3 - k1*magic2);

                yield return (byte) (keyByte & 0xff);

                index++;
            }
        }
    }
}
